<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );
class Upload extends CI_Controller
{

    public function __construct(){
        parent::__construct();
        $this->load->language('base');
        $this->load->service('login_service');
        $this->load->service('user_info_service');
        $this->load->service('user_files_service');
    }

    public function index()
    {
        echo "here";
        echo "here";
        echo "here";
    }

    /**
     * 检查上传是否合法
     * @param string file_name 文件名
     * @param string file_size
     * @param string file_mime_type
     */
    public function check()
    {
        //file_put_contents("test.txt",PHP_EOL."POST:{ ".json_encode($_POST)." } ".PHP_EOL."GET: { ".json_encode($_GET)." } ".PHP_EOL."REQUEST: { ".json_encode($_REQUEST)." } ".PHP_EOL."GET_POST: { ".json_encode($this->input->get_post())." } ".PHP_EOL);
        /*var_dump($this->input->get());
        var_dump($this->input->post());
        exit;*/
        $folder_id = $this->input->get_post("folder_id",TRUE);
        if($folder_id=="")
        {
            Common::ajax_return(1003);
        }
        $uid = $this->base_check(FALSE);
        //$file_name = $this->input->get_post("file_name",TRUE);
        $size = $this->input->get_post("file_size",TRUE);
        $size -= 1024;//预留1K的其他传参
        //$mime_type = $this->input->get_post("file_mime_type",TRUE);
        $can = $this->user_files_service->can_add_file_simple($uid,$size);
        if($can === FALSE)
        {
            Common::ajax_return($this->user_files_service);
        }
        else
        {
            Common::ajax_return(0);
        }
    }

    public function amqp()
    {
        $this->load->config("amqp");
        $cnn = new AMQPConnection($this->config->item("amqp"));
        $cnn->connect();
        if(!$cnn->isConnected()) {
            Common::ajax_return(1013);
        }
        $channel = new AMQPChannel($cnn);
        // Declare a new exchange
        $ex = new AMQPExchange($channel);
        //$ex->setName('packet_file_info');
        $ret = $ex->publish('{"file_id":"xxadfssadf-asdfasdf-asdfasd-fasdfasdf","file_name":"test"}', 'packet_request');
        var_dump($ret);
    }
    public function test()
    {
       /* echo json_encode(array(
            "uid"=>'用户的id',
            "guid"=>"文件的全局ID，此ID用于exe请求策略使用",
            "file_path"=>'上传文件的路径',
            "folder_id"=>'文件夹ID',
            "file_name"=>'文件名称',
            "file_md5"=>'文件MD5',
            "file_mime_type"=>'文件的mime type',
            "client_ip"=>'用户的ip地址',
            "secure_type"=>'打包类型',
            "source_guid"=>'源文件的dfs的guid',
            "source_exist"=>"源文件是否已经存在，如果已经存在就不需要存储到dfs",
            "secure_guid"=>'打包文件的dfs的guid'
        ));*/
        phpinfo();
        //require_once APPPATH.'third_party/MQ/vendor/autoload.php';
        //use PhpAmqpLib\Connection\AMQPConnection;
        //var_dump(class_exists("AMQPConnection"));
    }

    /**
     * 通知接口
     */
    public function notify()
    {

        $uid = $this->base_check(FALSE);
        //===== POST ========
        $file_path = $this->input->get_post("file_path",TRUE);
        if(!file_exists($file_path))
        {
            Common::ajax_return(1003);
        }
        $folder_id=$this->input->get_post("folder_id",TRUE);
        $file_name=$this->input->get_post("file_name",TRUE);
        $client_ip=$this->input->get_post("client_ip",TRUE);
        $secure_type=$this->input->get_post("secure_type",TRUE);
        if($secure_type=='')
        {
            $secure_type = 1;
        }
        /*$secure_guid=$this->input->post("secure_guid",TRUE);
        $secure_size=$this->input->post("secure_size",TRUE);
        $secure_md5=$this->input->post("secure_md5",TRUE);
        $secure_cipher=$this->input->post("secure_cipher",TRUE);*/
        //===== POST ========
        if($folder_id=="" || empty($file_name) || $client_ip=="")
        {
            Common::ajax_return(1003);
        }

        //$file_size=$this->input->post("file_size",TRUE);
        //$file_mime_type=$this->input->post("file_mime_type",TRUE);
        $mime_type = Common::get_mime_type($file_path);
        //$file_size = filesize($file_path);
        //$file_md5 = md5_file($file_path);
        $file_md5=$this->input->get_post("file_md5",TRUE);
        $file_size=$this->input->get_post("file_size",TRUE);
        $can = $this->user_files_service->can_add_file($uid,$file_name,$file_size,$mime_type);
        if($can === FALSE)
        {
            Common::ajax_return($this->user_files_service);
        }

        //$file_md5=$this->input->post("file_md5",TRUE);
        //$file_guid=$this->input->post("file_guid",TRUE);
        /*$source = $this->user_files_service->check_source_file_exists($file_md5);
        if($source)
        {
            $source_guid = $source['guid'];
        }
        else
        {
            $source_guid = Common::uniqid();
        }*/
        $source_guid = Common::uniqid();
        $guid = Common::uniqid();
        $secure_guid = Common::uniqid();
        $data = array(
            "uid"=>$uid,
            "file_guid"=>$guid,
            "file_path"=>$file_path,
            "folder_id"=>$folder_id,
            "file_name"=>$file_name,
            "file_md5"=>$file_md5,
            "file_mime_type"=>$mime_type,
            "file_size"=>$file_size,
            "client_ip"=>$client_ip,
            "secure_type"=>$secure_type,
            "source_guid"=>$source_guid,
            //"source_exist"=>$source?TRUE:FALSE,
            "secure_guid"=>$secure_guid
        );
        $data['xdebug_session']=$this->input->cookie("XDEBUG_SESSION");
        $package_info = json_encode($data);
        $this->load->config("amqp");
        $cnn = new AMQPConnection($this->config->item("amqp"));
        $cnn->connect();
        if(!$cnn->isConnected()) {
            Common::ajax_return(1013);
        }
        $channel = new AMQPChannel($cnn);
        // Declare a new exchange
        $ex = new AMQPExchange($channel);
        //$ex->setName('packet_file_info');
        $send = $ex->publish($package_info, 'packet_request');
        if(!$send)
        {
            Common::ajax_return(1014);
        }
        $redis = new Redis();
        $con = $redis->connect($this->config->item("host","redis"), $this->config->item("port","redis"), $this->config->item("timeout","redis"));
        if(!$con)
        {
            Common::ajax_return(1019);
        }
        //$this->redis->setex("testtest",10,"hahahah");
        $redis->set($guid,json_encode(array("percent"=>0,"data"=>NULL)));
        Common::ajax_return(0,NULL,array("guid"=>$guid),FALSE);
        //发送打包消息
    }

    /**
     * 基础判断
     * 失败过程直接返回json给客户端
     */
    private function base_check($exit)
    {
        if(!$this->login_service->is_login())
        {
            return Common::ajax_return($this->login_service);
        }
        $uid = $this->login_service->get_user_info("id");
        //判断用户空间是否已满
        if(!$this->user_info_service->space_enough($uid))
        {
            return Common::ajax_return($this->user_info_service);
        }
        //是否直接返回
        if($exit)
        {
            return Common::ajax_return();
        }
        else
        {
            return $uid;
        }
    }


    /**
     * 添加文件
     */
    public function add()
    {
        //用户id
        $uid = $this->input->get_post("uid",TRUE);
        //$uid = $this->base_check(FALSE);
       /* client_ip
            client_agent
            file_name
            file_size
            file_mime_type
            file_md5
            file_guid
       */
        //$client_ip=$this->input->post("client_ip",TRUE);
        $file_guid = $this->input->get_post("file_guid",TRUE);
        //$source_guid = $this->input->post("source_guid",TRUE);
        $folder_id=$this->input->get_post("folder_id",TRUE);
        $file_name=$this->input->get_post("file_name",TRUE);
        $file_size=$this->input->get_post("file_size",TRUE);
        $file_mime_type=$this->input->get_post("file_mime_type",TRUE);
        $file_md5=$this->input->get_post("file_md5",TRUE);
        $source_guid=$this->input->get_post("source_guid",TRUE);
        $client_ip=$this->input->get_post("client_ip",TRUE);
        $secure_type=$this->input->get_post("secure_type",TRUE);
        if($secure_type=='')
        {
            $secure_type = 1;
        }
        $secure_guid=$this->input->get_post("secure_guid",TRUE);
        $secure_size=$this->input->get_post("secure_size",TRUE);
        $secure_md5=$this->input->get_post("secure_md5",TRUE);
        $secure_cipher=$this->input->get_post("secure_cipher",TRUE);
        /*secure_type
        secure_guid
        secure_size
        secure_md5
        secure_cipher*/

        if($folder_id=="" || empty($file_name) || empty($file_size) || empty($file_mime_type) || empty($file_md5) || empty($file_guid) || empty($client_ip) || empty($secure_guid) || empty($secure_cipher))
        {
            return Common::ajax_return(1003);
        }

        $_SERVER['REMOTE_ADDR']=$client_ip;
        //$this->input->ip_address=$client_ip;

        $add_file = $this->user_files_service->add_file($uid,$file_name,$file_size,$file_mime_type,$file_md5,$source_guid,$folder_id,$file_guid);
        if($add_file===false)
        {
            return Common::ajax_return($this->user_files_service);
        }
        else
        {
            $add_sec = $this->user_files_service->add_secure_file($add_file['id'],$secure_size,$secure_type,$secure_guid,$secure_cipher,$secure_md5);
            if($add_sec===FALSE)
            {
                return Common::ajax_return($this->user_files_service);
            }
            if(!empty($this->input->get_post('ykf_guid',TRUE))&&!empty($this->input->get_post('ykf_size',TRUE))){
                $add_ek = $this->user_files_service->add_secure_file($add_file['id'],$this->input->get_post('ykf_size',TRUE),2,$this->input->get_post('ykf_guid',TRUE),$secure_cipher,$secure_md5);
                if($add_ek===FALSE)
                {
                    return Common::ajax_return($this->user_files_service);
                }
            }
            return Common::ajax_return(0,NULL,array("data"=>$add_file),FALSE);
        }
    }

    

}
